package com.dao.impl;

import com.core.dto.PageDTO;
import com.core.model.user.Administrator;
import com.core.model.user.User;
import com.dao.AdminDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 2016-01-08
 * Time: 11:15
 * To change this template use Editor | File and Code Templates.
 */
@Repository("adminDao")
public class AdminDaoImpl extends EntityDaoImpl implements AdminDao {


    @Override
    public List<User> findAllAdminUser(String currentPage) {
        String hql = "from " + Administrator.class.getName();
        Query query = currentSession().createQuery(hql);
        query.setCacheable(true);
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageSize);
        query.setMaxResults(PageDTO.pageSize);
        return query.list();
    }


    @Override
    public Administrator saveOrUpdateAdmin(Administrator administrator) {
        currentSession().save(administrator);
        return administrator;
    }

    @Override
    public Administrator findAdminByUsername(String username) {
        System.out.println("-----findAdminByUsername-----");
        String hql = "from " + Administrator.class.getName() + " a where a.username=:username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        List<Administrator> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void deleteAdmin(Administrator administrator) {
        currentSession().delete(administrator);
    }
}
